package com.ht.medicaltreatment.activity

import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.activity.viewModels
import com.blankj.utilcode.util.LogUtils

import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.utils.NFCUtils
import com.ht.medicaltreatment.utils.ScanUtils
import com.ht.medicaltreatment.utils.SoundUtils
import com.ht.medicaltreatment.utils.UIDUitls
import com.ht.medicaltreatment.viewmodel.SingleViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_nfc_layout.*

class NFCActivity:BaseActivity() {
    private var isNFC = false
    private val model by viewModels<SingleViewModel>()
    override fun getLayoutId() = R.layout.activity_nfc_layout

    override fun init() {
        //StatusBarCompat.setTranslucent(window,true)
        val packageManager = this.packageManager

        btnBack.setOnClickListener{
            finish()
        }

        if(Build.MODEL == "V9100"){
            ScanUtils.init()
            ScanUtils.open { uid ->
                SoundUtils.playSound(SoundUtils.SOUND_SUCCESS)
                ScanUtils.resetItem(uid)
                showProgress()
                model.wanDai(uid,false,{
                    dismissProgress()
                    val intent = Intent()
                    if (it.isAllCheckFinish){
                        intent.putExtra("patientId",it.patientInformation.id)
                        intent.setClass(this,CollectionRecordActivity::class.java)
                    }else{
                        intent.setClass(this,MultiSampleCollectionDetailActivity::class.java)
                        intent.putExtra("uid",uid)
                    }
                    ScanUtils.close()
                    startActivity(intent)
                    finish()
                },{
                    showToast(it)
                    dismissProgress()
                })
            }
        }else{
            isNFC = packageManager.hasSystemFeature(PackageManager.FEATURE_NFC)
            if(isNFC){
                NFCUtils(this)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if(!isNFC)return
        //开启前台调度系统
        NFCUtils.mNfcAdapter.enableForegroundDispatch(this,NFCUtils.mPendingIntent,NFCUtils.mIntentFilter,
            NFCUtils.mTechList)
    }

    override fun onPause() {
        super.onPause()
        if(!isNFC)return
        //关闭前台调度系统
        NFCUtils.mNfcAdapter.disableForegroundDispatch(this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        if(!isNFC)return
        //当该Activity接收到NFC标签时，运行该方法
        //调用工具方法，读取NFC数据
        var uid: String = NFCUtils.readNFCId(intent)
        LogUtils.i(uid)
        if(uid.length != 8){
            uid = UIDUitls.sort(uid)
        }
        val intent = Intent(this,MultiSampleCollectionDetailActivity::class.java)
        intent.putExtra("uid",uid)
        startActivity(intent)
    }

    override fun onDestroy() {
        super.onDestroy()
        ScanUtils.onDestroy()
    }
}